package com.tofine.smarthome.fragment;


import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.activeandroid.query.Select;
import com.tofine.smarthome.R;
import com.tofine.smarthome.adapter.FavoriteRecyclerViewAdapter;
import com.tofine.smarthome.base.BaseFragment;
import com.tofine.smarthome.model.Device;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author ddMax
 * @since 2016/4/25
 * 说明：收藏碎片类
 */
public class FavoriteFragment extends BaseFragment {
    public static final String TAG = FavoriteFragment.class.getSimpleName();

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    private Device[] testFavoriteData = {
            new Device(), new Device(),
            new Device()
    };

    private View view;
    @Bind(R.id.favorite_list) RecyclerView mFavoriteList;
    private RecyclerView.Adapter mAdapter;

    public FavoriteFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment FavoriteFragment.
     */
    public static FavoriteFragment newInstance() {
        FavoriteFragment fragment = new FavoriteFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO: 收藏Fragment页面
        view = inflater.inflate(R.layout.fragment_favorite, container, false);
        initView();
        return view;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this, view);

        setUpActionBar();
        setUpFavoriteList();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void setUpActionBar() {
        super.setUpActionBar();
        mTvActionBarTitle.setText(getString(R.string.title_fragment_favorite));
        mActionBar.getCustomView().findViewById(R.id.btn_add_device).setVisibility(View.INVISIBLE);
    }

    private void setUpFavoriteList() {
        mFavoriteList.setLayoutManager(new LinearLayoutManager(getActivity()));
        // 设置Adapter
        List<Device> favoriteData = new Select().from(Device.class).where("favorite").execute();
        mAdapter = new FavoriteRecyclerViewAdapter(getActivity(), favoriteData);
        mFavoriteList.setAdapter(mAdapter);
    }

}
